<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    // 比如，可以将被代理的类绑定到一个全局实例集合，让所有创建的实例都被添加到这个集合中:
    const userList = [];

    class User {
        constructor(name) {
            this.name_ = name;
        }
    }

    const proxy = new Proxy(User, {
        construct() {
            const newUser = Reflect.construct(...arguments);
            userList.push(newUser);
            return newUser;
        }
    });
    new proxy('John');
    new proxy('Jacob');
    new proxy('Jingleheimerschmidt');
    console.log(userList); // [User {}, User {}, User{}]


    /*
        2.还可以把集合绑定到一个事件分派程序，每次插入新实例时都会发送消息:
     */
    const userList2 = [];

    function emit(newValue) {
        console.log(newValue);
    }

    const proxy2 = new Proxy(userList2, {
        set(target, property, value, receiver) {
            const result = Reflect.set(...arguments);
            if (result) {
                emit(Reflect.get(target, property, receiver));
            }
            return result;
        }
    });
    proxy2.push('John'); // John proxy.push('Jacob'); // Jacob

</script>
</body>
</html>